package com.example.demo.config;

import com.example.demo.common.CodeMessageEnum;
import com.example.demo.common.DemoException;
import com.example.demo.common.Response;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JiJunpeng
 * @date 2018/7/25
 */
@ControllerAdvice
@RestController
public class GlobalController {

    @ExceptionHandler(DemoException.class)
    public Response demoExceptionHandler(DemoException e) {
        String code = e.getCodeMessage().getCode();
        String message = e.getMessage();
        return new Response(code, message);
    }

    @ExceptionHandler(Throwable.class)
    public Response defaultExceptionHandler(Throwable throwable) {
        return new Response(CodeMessageEnum.SYSTEM_ERROR);
    }
}
